package com.wenshi.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;


import com.wenshi.app.activity.R;
import com.wenshi.app.adapter.WsViewAdapter;
import com.wenshi.view.IKeyValue;
import com.wenshi.view.WsViewTools;

import java.util.ArrayList;




/**
 * 该类适用于带listview的布局使用  直接extends该类即可
 */
public abstract class WsListViewActivity extends WsBaseActivity {
    private Context mContext;
    protected WsViewAdapter adapter;
    private ListView wslistview;
    private boolean hassetadapter = false;
    ArrayList<Integer> header_ids = new ArrayList<>();
    ArrayList<Integer> header_ids1 = new ArrayList<>();
    ArrayList<IKeyValue> header_ids_obj = new ArrayList<>();
    ArrayList<IKeyValue> header_ids1_obj = new ArrayList<>();
    private boolean renderView_ok = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("rlr","onCreate");
        mContext = this;
        super.onCreate(savedInstanceState);

    }


    protected void initLayOutAndListView() {
        Log.e("rlr",renderView_ok+"");
        if (!renderView_ok) {
            getWsWiewDelegate().setContentView(R.layout.wsview_activity_pub_wslistview );
            wslistview = (ListView) findViewById(  R.id.ws_listview);
            if (wslistview!=null)
            {
                Log.e("rlr","eeeeeee");
            }
            Log.e("rlr","testest");
            for (Integer id : header_ids) {
                View tmp = getLayoutInflater().inflate(id, null);
                WsViewTools.renderView(this, tmp, header_ids_obj.get(id));
                getListView().addHeaderView(tmp);
            }
            for (Integer footid : header_ids1) {
                View tmp = getLayoutInflater().inflate(footid, null);
                WsViewTools.renderView(this, tmp, header_ids1_obj.get(footid));
                getListView().addFooterView(tmp);
            }
            renderView_ok = true;
        }
    }

    /**
     * 子类使用时用来获取该类的 listview控件
     */
    public ListView getListView() {
        return wslistview;
    }

    /**
     * 将数据通过此方法赋值给listview下的item
     * 参数是ArrayList<HashMap<String, String>> items， int  id
     */
    protected void initAdapterData(ArrayList<IKeyValue> items, int id) {
        this.initLayOutAndListView();
        if (adapter == null) {
            adapter = new WsViewAdapter(mContext, id, null);
        }
        adapter.initData(items);
        whenloaddata(items, true);
    }

    protected void initAdapterData(ArrayList<IKeyValue> items, int id, WsViewAdapter.IWsViewRetView retView) {
        this.initLayOutAndListView();
        if (adapter == null) {
            adapter = new WsViewAdapter(mContext, id, null);
        }
        adapter.initData(items);
        adapter.setResetViewListener(retView);
        whenloaddata(items, true);
    }

    /**
     * .对于特殊情况时使用
     */
    protected void initAdapterData(ArrayList<IKeyValue> items) {
        this.initLayOutAndListView();
        if (adapter != null) {
            adapter.initData(items);
            whenloaddata(items, true);
        }

    }

    /**
     * 该方法用于多布局的使用
     * 根据类型的判断 来决定使用哪种布局
     * 参数 ArrayList<HashMap<String, String>> items，WsViewAdapter.IWsViewGetLayoutId getLayoutId（要使用的布局）
     */
    protected void initAdapterData(ArrayList<IKeyValue> items, WsViewAdapter.IWsViewGetLayoutId getLayoutId) {
        this.initLayOutAndListView();
        if (adapter == null) {
            adapter = new WsViewAdapter(mContext, null, getLayoutId);
        }
        adapter.initData(items);
        whenloaddata(items, true);
    }

    protected void initAdapterData(ArrayList<IKeyValue> items, WsViewAdapter.IWsViewGetLayoutId getLayoutId, WsViewAdapter.IWsViewRetView retView) {
        this.initLayOutAndListView();
        if (adapter == null) {
            adapter = new WsViewAdapter(mContext, null, getLayoutId);
        }
        adapter.initData(items);
        adapter.setResetViewListener(retView);
        whenloaddata(items, true);
    }

    protected void whenloaddata(ArrayList<IKeyValue> items, boolean isinit) {
      //  this.initLayOutAndListView();
        if (!hassetadapter) {
            getListView().setAdapter(adapter);
            hassetadapter = true;
        } else {
            adapter.notifyDataSetChanged();
        }

    }

    /**
     * 添加listvie的头布局
     * 参数 int id
     */
    protected void addHeader(int id, IKeyValue object) {

        header_ids.add(id);
        header_ids_obj.add(object);
    }

    protected void addFoot(int footid, IKeyValue object) {
        header_ids1.add(footid);
        header_ids1_obj.add(object);

    }


}
